<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>线路管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">线路编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="circuit_number" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">线路状态：</label>
                    <div class="layui-input-block">
                        <select name="operation_status">
                            <option value="" selected>--请选择--</option>
                            <option value="0">正常</option>
                            <option value="1">检修中</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>

                </div>

            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加线路</button>
            </div>
            <div style="align-text: center">
            <!--动态数据表格-->
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
            <!--状态判断-->
            <script type="text/html" id="runstatusTpl">
                <div id="wrap">
                {{#  if(d.startusing_status == 0){ }}
                <button class="layui-btn layui-btn-xs">正常</button>
                {{#  } else if(d.startusing_status == 1) { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">检修中</button>
                {{#  } }}
                </div>
            </script>
                <script type="text/html" id="statusTpl">
                    {{#  if(d.operation_status == 0){ }}
                    <button class="layui-btn layui-btn-normal layui-btn-xs">启用</button>
                    {{#  } else if(d.operation_status == 1) { }}
                    <button class="layui-btn layui-btn-danger layui-btn-xs">停用</button>
                    {{#  } }}
                </script>
            <script type="text/html" id="table-useradmin-webuser">
                    {{#  if(d.operation_status == 1){ }}
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="lock">启用</a>
                    {{#  } else if(d.operation_status == 0) { }}
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="lock">停用</a>
                    {{#  } }}
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" data-type="delete">删除</a>
            </script>
        </div>>
        </div>
    </div>
</div>

<script src="../../js/jquery-3.4.1.min.js"></script>
<script src="../../layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['layer','table','form'], function(){
        var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,table = layui.table;

        //加载table
        table.render({
            elem: '#LAY-user-manage'
            ,url:'/circuit/pageCircuit'
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度
            ,title: '线路数据表'
            ,cols: [[
                {field:'circuit_number', title:'线路编号',sort:'true',align:'center'}
                ,{field:'name', title:'线路名称',align:'center'}
                ,{field:'init_pole', title:'起始杆号',align:'center'}
                ,{field:'end_pole', title:'终止杆号',align:'center'}
                ,{field:'columnfoot_number', title:'塔基数',align:'center'}
                ,{field:'operation_status', title:'运行状态',templet: '#runstatusTpl',align:'center'}
                ,{field:'startusing_status', title:'启用状态', templet:'#statusTpl',align:'center'}
                ,{fixed: 'right', title:'操作', toolbar: '#table-useradmin-webuser',width:300,align:'center'}
            ]]
            ,page: true
        });

        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('LAY-user-manage', {
                where: field
            });
        });

        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加线路'
                    ,content: 'operation/lineform.html'
                    ,maxmin: true
                    ,area: ['460px', '570px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            console.log(field)
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:"/circuit/saveCircuit",
                                type:"post",
                                data:field,
                                dataType:"json",
                                success:function (res) {
                                    if(res.code == 200){
                                        layer.msg(res.msg);
                                        //执行重载
                                        table.reload('LAY-user-manage', {
                                            where: {
                                                key: {
                                                    id: new Date().getTime()
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                            table.reload('#LAY-user-front-submit'); //数据刷新
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                });
            }

        };

        //监听行工具事件
        table.on('tool(LAY-user-manage)', function(obj){
            var data = obj.data;
            if(obj.event === 'lock'){
                layer.confirm('确定修改吗？', function(index) {
                    var idss=data.id;
                    $.ajax({
                        url:`/circuit/updateStartusing/${idss}`,
                        type:"post",
                        success:function(res){
                            if(res.code == 200){
                                layer.msg(res.msg);
                                console.log(data);
                                //执行重载
                                table.reload('LAY-user-manage', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                    ,where: {
                                        key: {
                                            id: new Date().getTime()
                                        }
                                    }
                                });

                            }
                            if(res.code == 500){
                                layer.msg(res.msg);
                            }
                        },
                    })
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '修改线路'
                    ,content: 'operation/editform.html'
                    ,maxmin: true
                    ,area: ['460px', '570px']
                    ,btn: ['确定', '取消']
                    ,success: function(layero, index){//在页面加载成功之前将数据回显
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = layero.find('iframe')[0].contentWindow; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        body.find('input[name=id]').val(data.id);
                        body.find('input[name=circuit_number]').val(data.circuit_number);
                        body.find('input[name=name]').val(data.name);
                        body.find('input[name=init_pole]').val(data.init_pole);
                        body.find('input[name=end_pole]').val(data.end_pole);
                        body.find('input[name=columnfoot_number]').val(data.columnfoot_number);
                        body.find('#operation_status input[value='+data.operation_status+']').attr("checked","checked");
                        body.find('#startusing_status input[value='+data.startusing_status+']').attr("checked","checked");
                        //重载
                        iframeWin.layui.form.render("radio");
                        iframeWin.layui.form.render("select");
                    }
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:"/circuit/updateCircuit",
                                type:"post",
                                data:field,
                                dataType:"json",
                                success:function (res) {
                                    if(res.code == 200){
                                        layer.msg(res.msg);
                                        console.log(data)
                                        //执行重载
                                        table.reload('LAY-user-manage', {
                                            where: {
                                                key: {
                                                    id: new Date().getTime()
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            } else if(obj.event === 'del'){
                var data = obj.data;
                layer.confirm('确定删除吗？', function(index) {
                    //定义空数组
                    var ids = data.id;
                    //通过Ajax访问后台实现删除功能
                    $.ajax({
                        url:`/circuit/deleteCircuit/${ids}`,
                        type:"delete",
                        traditional:true, //用传统的方式来序列化数据
                        success:function(res){
                            if(res.code == 200){
                                layer.msg(res.msg);
                                //执行重载
                                table.reload('LAY-user-manage', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                    ,where: {
                                        key: {
                                            id: new Date().getTime()
                                        }
                                    }
                                });
                            }
                            if(res.code == 500){
                                layer.msg(res.msg);
                            }
                        }
                    })

                });
            }
        });
        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>
</html>
